<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends CommonController {
	public function index(){
		$sql = "select * from auth_rule_category";
		$cateRes = M()->query($sql);
		$this->assign('cateRes',$cateRes);
		$this->display();
	}

	//分类编辑
	public function cateedit(){
		if(IS_GET){
			$cateid = I('get.cateid');
			$sql = "select * from auth_rule_category where id = {$cateid}";
			$res = M()->query($sql);
			$this->assign('cateData',$res);
			$this->display();
		}elseif(IS_POST){
			$cateid = I('post.cateid');
			$catename = I('post.cname');
			$sql = "update auth_rule_category set cname = '{$catename }' where id = {$cateid}";
			$res = M()->execute($sql);
			if($res){
				$this->success('修改成功',U('admin/cate/index'));
			}else{
				$this->error('修改失败');
			}
		}
	}

	//分类删除
	public function catedelete(){
		if($_GET){
			$id = I('get.cateid');
			//删除管理员的操作
			$sql = "delete from auth_rule_category where id = {$id}";
			if(M()->execute($sql)){
				$this->success('删除成功',U('admin/cate/index'));
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('非法操作');
		}
	}

	//分类添加
	public function cateadd(){
		if(IS_POST){
			$cname = $_POST['cname'];
			$sql = "insert into auth_rule_category(cname) values('{$cname}')";
			$res = M()->execute($sql);
			if($res){
				$this->success('添加成功',U('admin/cate/index'));
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->display();
		}
		
	}

	public function catemutipledelete(){
		if(IS_POST){
			$cateid = $_POST['cateid'];
			foreach ($cateid as  $value) {
				$sql = "delete from auth_rule_category where id = {$value}";
				M()->execute($sql);
			}
			$this->success('删除成功',U('admin/cate/index'));
		}
	}
}